package io.renren.modules.department.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.modules.department.model.DepartmentBo;
import io.renren.modules.department.model.DepartmentEntity;
import io.renren.modules.department.model.DepartmentVo;
import io.renren.modules.department.service.DepartmentService;
import io.renren.modules.sys.entity.SysMenuEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-18 15:23:46
 */
@RestController
@RequestMapping("/business/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("business:department:list")
    public R list() {
        List<DepartmentVo> departmentVoList = departmentService.queryList();
        return R.ok().put("data", departmentVoList);
    }


    /**
     * 根据主键id查询部门信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:department:info")
    public R info(@PathVariable("id") Integer id) {
        DepartmentEntity businessDepartment = departmentService.getById(id);
        return R.ok().put("businessDepartment", businessDepartment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("generator:businessdepartment:save")
    public R save(@RequestBody DepartmentBo departmentBo) {
        departmentService.save(departmentBo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("generator:businessdepartment:update")
    public R update(@RequestBody DepartmentBo departmentBo) {
        departmentService.updateById(departmentBo);
        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除部门")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("business:department:delect")
    public R delete(@PathVariable("id") long id){

        //判断是否有子部门
        List<DepartmentEntity> menuList = departmentService.queryListParentId(id);
        if(menuList.size() > 0){
            return R.error("请先删除子部门");
        }
        List<Integer> idList=new ArrayList<>();
        idList.add((int) id);
        departmentService.removeByIds(idList);
        return R.ok();
    }

}
